package com.auth.service.impl;

import com.auth.entity.dao.Authority;
import com.auth.entity.dao.UserRole;
import com.auth.entity.dto.AuthorityDto;
import com.auth.entity.vo.AuthorityVo;
import com.auth.mapper.AuthorityMapper;
import com.auth.service.IAuthorityService;
import com.auth.service.IRoleAuthorityService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easyCode.feature.mybaits.CustomPage;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static com.auth.entity.factory.AuthorityFactory.AUTHORITY_CONVERTER;

@Service
@RequiredArgsConstructor
public class AuthorityServiceImpl extends ServiceImpl<AuthorityMapper, Authority> implements IAuthorityService {
    @Autowired
    private IRoleAuthorityService iRoleAuthorityService;

    @Override
    public Integer saveAuthority(AuthorityVo authorityVo) {
        Authority authority = AUTHORITY_CONVERTER.toAuthority(authorityVo);
        save(authority);
        return authority.getId();
    }

    @Override
    public AuthorityDto getAuthorityById(Integer id) {
        Authority authority = getById(id);
        return AUTHORITY_CONVERTER.toAuthorityDto(authority);
    }

    @Override
    public List<AuthorityDto> listAuthority(CustomPage customPage) {
        List<Authority> authorityList = CustomPage.getResult(customPage, new Authority(), this, null);
        return AUTHORITY_CONVERTER.toListAuthorityDto(authorityList);
    }

    @Transactional
    @Override
    public void delAuthorityById(Integer id) {
        removeById(id);
        iRoleAuthorityService.removeByAuthorityId(id);
    }
}
